// manager/pages/visit/index.js
import config from '../../../utils/config';
import { request } from '../../../utils/request';
import util from '../../../common/util';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseUrl: config.baseUrl,
        // 商户id
        merchantId: "",
        // 1-个人，2-商户，3-企业，4-新型农业主体
        merchantType: "",
        // 业务字典
        dict: {},
        // 已开通业务
        openList: [],
        // 营销重点
        keynoteList: [],
        personDetail: {},
        merchantDetail: {},
        enterpriseDetail: {},
        agricultureDetail: {},
        visitList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            merchantId: options.merchantId,
            merchantType: options.merchantType,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if(this.data.merchantType == 1){
            this.getPersonInfo();
        }
        if(this.data.merchantType == 2){
            this.getMerchantInfo();
        }
        if(this.data.merchantType == 3){
            this.getEnterpriseInfo();
        }
        if(this.data.merchantType == 4){
            this.getAgricultureInfo();
        }
        this.getMerchantVisitList();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 获取字典
     */
    getDictionaryInfo(dictType) {
        request({
            url: "/commonApi/getDictionaryInfo",
            method: "GET",
            data: {
                dictType
            }
        }).then(res => {
            if(res.code == 0){
                if([
                    'ecard_person_business_tag',
                    'ecard_merchant_business_tag',
                    'ecard_enterprise_business_tag',
                    'ecard_agriculture_business_tag'
                ].indexOf(dictType) != -1){
                    this.setData({
                        busiList: res.data
                    })
                    this.getOpenAndKeynoteList(res.data);
                }else{
                    this.setData({
                        ['dict.'+dictType]: res.data
                    })
                }
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    // 获取已开通、营销重点
    getOpenAndKeynoteList(list){
        if(this.data.merchantType == 1){
            let openList = [];
            let keynoteList = [];
            let barr = this.data.personDetail.perBusinessLabel?this.data.personDetail.perBusinessLabel.split(","):[];
            let farr = this.data.personDetail.perMarketingFocus?this.data.personDetail.perMarketingFocus.split(","):[];
            list.forEach(item => {
                if(barr.indexOf(item.dictValue)!=-1){
                    openList.push(item)
                }
                if(farr.indexOf(item.dictValue)!=-1){
                    keynoteList.push(item)
                }
            })
            this.setData({
                openList,
                keynoteList
            })
        }
        if(this.data.merchantType == 2){
            let openList = [];
            let keynoteList = [];
            let barr = this.data.merchantDetail.businessLabel?this.data.merchantDetail.businessLabel.split(","):[];
            let farr = this.data.merchantDetail.marketingFocus?this.data.merchantDetail.marketingFocus.split(","):[];
            list.forEach(item => {
                if(barr.indexOf(item.dictValue)!=-1){
                    openList.push(item)
                }
                if(farr.indexOf(item.dictValue)!=-1){
                    keynoteList.push(item)
                }
            })
            this.setData({
                openList,
                keynoteList
            })
        }
        if(this.data.merchantType == 3){
            let openList = [];
            let keynoteList = [];
            let barr = this.data.enterpriseDetail.entBusinessLabel?this.data.enterpriseDetail.entBusinessLabel.split(","):[];
            let farr = this.data.enterpriseDetail.entMarketingFocus?this.data.enterpriseDetail.entMarketingFocus.split(","):[];
            list.forEach(item => {
                if(barr.indexOf(item.dictValue)!=-1){
                    openList.push(item)
                }
                if(farr.indexOf(item.dictValue)!=-1){
                    keynoteList.push(item)
                }
            })
            this.setData({
                openList,
                keynoteList
            })
        }
        if(this.data.merchantType == 4){
            let openList = [];
            let keynoteList = [];
            let barr = this.data.agricultureDetail.agrBusinessLabel?this.data.agricultureDetail.agrBusinessLabel.split(","):[];
            let farr = this.data.agricultureDetail.agrMarketingFocus?this.data.agricultureDetail.agrMarketingFocus.split(","):[];
            list.forEach(item => {
                if(barr.indexOf(item.dictValue)!=-1){
                    openList.push(item)
                }
                if(farr.indexOf(item.dictValue)!=-1){
                    keynoteList.push(item)
                }
            })
            this.setData({
                openList,
                keynoteList
            })
        }
    },
    // 个人信息
    getPersonInfo(){
        request({
            url: "/personMarketing/getPersonInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                id: this.data.merchantId
            }
        }).then(res => {
            if(res.code == 0){
                res.data.creationTime = util.formatTime(new Date(res.data.creationTime));
                this.setData({
                    personDetail: res.data
                })
                this.getDictionaryInfo('ecard_person_business_tag');
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    // 商户信息
    getMerchantInfo(){
        request({
            url: "/merchantMarketing/getMerchantInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                id: this.data.merchantId
            }
        }).then(res => {
            if(res.code == 0){
                res.data.creationTime = util.formatTime(new Date(res.data.creationTime));
                this.setData({
                    merchantDetail: res.data
                })
                this.getDictionaryInfo('ecard_merchant_business_tag');
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    // 企业信息
    getEnterpriseInfo(){
        request({
            url: "/enterpriseMarketing/getEnterpriseInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                id: this.data.merchantId
            }
        }).then(res => {
            if(res.code == 0){
                res.data.creationTime = util.formatTime(new Date(res.data.creationTime));
                this.setData({
                    enterpriseDetail: res.data
                })
                this.getDictionaryInfo('ecard_enterprise_business_tag');
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    // 新型农业主体信息
    getAgricultureInfo(){
        request({
            url: "/agricultureMarketing/getAgricultureInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                id: this.data.merchantId
            }
        }).then(res => {
            if(res.code == 0){
                res.data.creationTime = util.formatTime(new Date(res.data.creationTime));
                this.setData({
                    agricultureDetail: res.data
                })
                this.getDictionaryInfo('ecard_agriculture_business_tag');
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    
    /** 跳转商户详情 */
    toAddMerchant(e) {
        if(this.data.merchantType == 1){
            wx.navigateTo({
                url: `/manager/pages/persondesc/index?personId=${this.data.merchantId}`,
            })
        }
        if(this.data.merchantType == 2){
            wx.navigateTo({
                url: `/manager/pages/merchantdesc/index?merchantId=${this.data.merchantId}`,
            })
        }
        if(this.data.merchantType == 3){
            wx.navigateTo({
                url: `/manager/pages/enterprisedesc/index?enterpriseId=${this.data.merchantId}`,
            })
        }
        if(this.data.merchantType == 4){
            wx.navigateTo({
                url: `/manager/pages/agriculturedesc/index?agricultureId=${this.data.merchantId}`,
            })
        }
    },
    // 拜访列表
    getMerchantVisitList(){
        request({
            url: "/merchantMarketing/getMerchantVisitList",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                merchantId: this.data.merchantId,
                merchantType: this.data.merchantType,
            }
        }).then(res => {
            if(res.code == 0){
                if(res.data.length>0){
                    res.data.map(item => {
                        item.creationTime = util.formatTime(new Date(item.creationTime));
                        return item
                    })
                }
                this.setData({
                    visitList: res.data
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    /** 跳转拜访详情 */
    toVisitDetail(e){
        let { merchantid, id } = e.currentTarget.dataset;
        wx.navigateTo({
          url: `/pages/grid/visitdesc/index?merchantId=${merchantid}&visitId=${id}`,
        })
    },
    // 查看图片
    previewImage(e) {
        let { index,url } = e.currentTarget.dataset;
        let arr = this.data.visitList[index].visitPic?this.data.visitList[index].visitPic.split(","):[];
        let images = [];
        arr.forEach(item => {
            images.push(this.data.baseUrl+item)
        })
        wx.previewImage({
            current: url, // 当前显示图片的http链接
            urls: images // 需要预览的图片http链接列表
        })
    },
    // 跳转成员列表
    toMember(e){
        wx.navigateTo({
          url: `/manager/pages/merchantmember/index?merchantId=${this.data.merchantId}`,
        })
    },
    // 跳转添加拜访
    toAddVisit(e){
        wx.navigateTo({
          url: `/manager/pages/addvisit/index?merchantId=${this.data.merchantId}&merchantType=${this.data.merchantType}`,
        })
    },
})